<template>
  <div class="roomInfoParticulars">
    <div class="posterContainer">
      <div class="blackBorder">
        <div class="whiteBorder">
          <img :src="roomInfo.image" alt="" />
        </div>
      </div>
    </div>

    <div class="contentContainer">
      <div class="particulars">
        <div class="habitat" style="margin-top: 20px">
          <i class="label">自习室名称:</i>
          <i class="info" v-html="roomInfo.name"></i>
        </div>
        <div class="habitat" style="margin-top: 20px">
          <i class="label">自习室价格详情:</i>
          <i class="info" v-html="roomInfo.charge" style="color: #ad7d7d"></i>
        </div>
        <div class="protectGrade" style="margin-top: 20px">
          <i class="label">人数限制:</i>
          <i class="info"
            ><el-tag type="danger" size="large">{{ roomInfo.people_ceiling }}人</el-tag></i
          >
        </div>
        <div class="protectGrade" style="margin-top: 20px">
          <i class="label">已经预约人数:</i>
          <i class="info"
            ><el-tag type="info" size="large"
              >{{ roomInfo.reserve_people }}人
              {{
                roomInfo.reserve_people >= roomInfo.people_ceiling ? "(已经预约满了)" : ""
              }}</el-tag
            ></i
          >
        </div>
        <div class="habitat">
          <i class="label">自习室设备描述:</i>
          <i class="info" v-html="roomInfo.facilities"></i>
        </div>
        <div class="habitat" style="margin: 20px 0">
          <i class="label">自习室详细描述:</i>
          <i class="info" v-html="roomInfo.describes"></i>
        </div>
      </div>

      <div class="appointment" style="margin: 40px 0">
        <i class="label">自习室预约:</i>
        <div class="form">
          <div class="date">
            <el-date-picker v-model="reservation_date" type="date" placeholder="选择预约的日期" />
          </div>
          <div class="time">
            <el-time-picker
              style="margin-right: 10px"
              v-model="start_time"
              placeholder="起始预约时间"
            />
            <el-time-picker v-model="end_time" placeholder="结束预约时间" />
          </div>
          <div class="predictPrice">
            <span>预计金额：</span>
            <i>￥{{ predictPrice }}</i>
          </div>
          <div class="bottom">
            <el-button @click="close">清空</el-button>
            <el-button
              type="primary"
              @click="save"
              :disabled="roomInfo.reserve_people >= roomInfo.people_ceiling || !timeDiff"
              >确定</el-button
            >
          </div>
        </div>
      </div>
    </div>

    <getScroll></getScroll>
    <getBack></getBack>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from "vue"
import getScroll from "@/components/UI-cnps/getScroll/getScroll.vue"
import getBack from "@/components/UI-cnps/getBack/getBack.vue"
import { localCache } from "@/utils/cacheData"
import { onUnmounted } from "vue"
import { formatDate } from "@/utils/format"
import useRouterJump from "@/hooks/useRouterJump"
import useStudyRoomStore from "@/store/main/studyRoom/studyRoom"
import { InnerNotify, messageReminder } from "@/utils/notificationHint"

const roomInfo = localCache.getCache("roomInfo")
console.log(roomInfo)

const reservation_date = ref()
const start_time = ref()
const end_time = ref()

const { userInfo, noLoginInnerNotify } = useRouterJump()

const studyRoomStore = useStudyRoomStore()

// 一个计算属性 ref

const timeDiff = computed(() => {
  // 获取开始时间和结束时间的值
  const startValue = start_time.value
  const endValue = end_time.value

  // 确保开始时间和结束时间有效
  if (!startValue || !endValue) return 0

  // 将字符串转换为日期对象
  const start = new Date(startValue)
  const end = new Date(endValue)
  // 计算时间差（毫秒数）
  const timeDiff = end - start
  if (timeDiff <= 0) return 0
  return timeDiff
})
const predictPrice = computed(() => {
  // 检查 roomInfo.charge 是否包含数字
  const numbers = roomInfo.charge.match(/\d+/g)
  if (!numbers || numbers.length === 0) return 0

  // 获取收费标准
  const charge = parseFloat(numbers[0])

  // 将毫秒数转换为小时数
  const hours = Math.ceil(timeDiff.value / (1000 * 60 * 60))

  // 计算总价
  const totalPrice = charge * hours

  return totalPrice
})

function save() {
  if (!userInfo) {
    noLoginInnerNotify()
  } else {
    console.log(123)
    const data = {
      room_uid: roomInfo.user.id,
      room_id: roomInfo.id,
      reservation_date: formatDate(reservation_date.value, true),
      start_time: formatDate(start_time.value).nowTime,
      end_time: formatDate(end_time.value).nowTime,
      money: predictPrice.value
    }
    console.log(data)

    studyRoomStore.appointmentStudyRoom(data).then(() => {
      InnerNotify({
        message: "预约成功！",
        type: "success",
        position: "bottom-right"
      })
      reservation_date.value = ""
      start_time.value = ""
      end_time.value = ""
    })
  }
}

function close() {
  reservation_date.value = ""
  start_time.value = ""
  end_time.value = ""
}

// formatDate
</script>

<style lang="less" scoped>
.roomInfoParticulars {
  .posterContainer {
    width: 1000px;
    padding: 0 100px;
    margin: 24px auto;
    .blackBorder {
      margin: 0 auto;
      width: 600px;
      // 只给宽 高度自适应
      border: 8px solid #343436;
      width: 600px;

      -webkit-box-shadow: 0 13px 19px -1px hsl(0deg 0% 47% / 75%);
      box-shadow: 0 13px 19px -1px hsl(0deg 0% 47% / 75%);
      img {
        // 边框盒子模型
        box-sizing: border-box;
        border: 8px solid #fff;
        width: 100%;
        height: 100%;
      }
    }
  }

  .contentContainer {
    width: 1000px;
    // height: 1000px;
    background-color: #fff;
    margin: 0 auto;
    padding: 0 100px 140px 100px;
    text-align: center;
    overflow: hidden;

    /* 名字 */
    .animalName {
      margin-top: 100px;
      .name {
        font-size: 120px;
        color: #333333;
        letter-spacing: 2px;
        // font-weight: 600;
      }
      .alias {
        color: #ad7d7d;
        display: inline-block;
        width: 600px;
        margin-top: 20px;
        font-size: 30px;
      }

      .scientificName {
        color: #ad7d7d;
        display: inline-block;
        width: 600px;
        font-weight: 500;
        margin-top: 20px;
        font-size: 26px;
      }
    }

    div .label {
      // color: #333333;
      color: #0e151c;
      vertical-align: middle;
      font-size: 28px;
      // line-height: 28px;
      letter-spacing: 1px;
      margin-right: 12px;
    }
    div .info {
      font-size: 20px;
      vertical-align: middle;
      letter-spacing: 1px;
      line-height: 40px;
    }

    .particulars {
      // margin: 60px 0 0 30px;
      // display: flex;
      // flex-direction: column;
      margin-top: 60px;
      text-align: left;

      .protectGrade,
      .endangeredLevel,
      .distributionArea,
      .animalClassification {
        margin-bottom: 20px;
        .label {
          color: #0e151c;
          font-weight: 600;
        }
      }

      .habitat {
        // border-bottom: #999;
        .label {
          color: #0e151c;
          font-weight: 600;
        }
      }
    }

    .model {
      // 消除父元素左右padding的影响
      margin: 0 -100px;
    }

    .introduce {
      margin-top: 60px;
      text-align: left;
      .habits {
        margin-bottom: 20px;
      }
      .morphology {
        display: flex;
        margin-bottom: 20px;
        padding-top: 36px;
        border-top: 1px solid #eee;
        .label {
          margin-right: 20px;
        }
      }
      .status {
        display: flex;
        margin-bottom: 20px;
        padding-top: 36px;
        border-top: 1px solid #eee;
        .label {
          // width: 20%;
          margin-right: 20px;
        }
        .info {
          .populationStatus {
            margin-bottom: 10px;
            i {
              font-weight: 600;
            }
          }

          .elseInfo {
            i {
              font-weight: 600;
            }
          }
        }
      }
    }
  }
}

.appointment {
  .label {
    color: #0e151c;
    font-weight: 600;
  }
  .form {
    .time,
    .date,
    .bottom {
      margin-top: 20px;
    }
    .predictPrice {
      margin-top: 20px;
      font-size: 40px;
      span {
        color: #999;
      }
      i {
        color: red;
      }
    }
  }
}

.el-tag {
  margin-right: 16px;
  font-size: 14px;
  // font-weight: 600;
}
</style>
